﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEBDT.Base;
using WEBDT.ViewModel;
namespace WEBDT.Areas.Front.Controllers
{
    public class AccountController : BaseController<Account>
    {
        //
        // GET: /Front/Account/
        [HttpGet]
        public ActionResult register()
        {
            var ht = new AccountModel 
            {
                tontai= false,
            };
            return View(ht);
        }
        [HttpPost]
        public ActionResult register(FormCollection fr)
        {
            var ht = new AccountModel 
            {
                Username = fr.Get("username"),
                Password = fr.Get("password"),
                confirmPass = fr.Get("confirm_password")
            };
            if (ht.Password != ht.confirmPass)
                return View(ht);
            var model = new Account
            {
                Username = ht.Username,
                Password = ht.Password,
                MaUserPower= 4,
                Status = 0,
            };
           
            repository.Insert<Account>(model);
            return View();
        }
        [HttpGet]
        public ActionResult LogOn()
        {
            var model = new AccountModel
            {
                tontai= false,
            };
            return View(model);
        }
        [HttpPost]
        public ActionResult LogOn(FormCollection fr)
        {
            var user = new AccountModel
            {
                Username = fr.Get("username"),
                Password = fr.Get("password")
            };
            var model = repository.validateUser(user.Username, user.Password);
            if (model == null)
            {
                return RedirectToAction("ChangePass");
            }
            Session["user"] = model.Username;
             Session["quyen"] = model.MaUserPower;
            int quyen = model.MaUserPower;
            if(quyen < 4)
                return RedirectToAction("getall", "dienthoai", new { area = "admin" });
            else
                return RedirectToAction("index","index");
        }

        public ActionResult LogOff()
        {
            Session["user"] = null;
            Session["quyen"] = null;
            return View();
        }
        [HttpGet]
        public ActionResult ChangePass(string id)
        {
            var model = repository.SelectByID1<Account>(id);

            var ht = new AccountModel
            {
                Username= model.Username,
                Password= model.Password,   
            };
            return View(ht);
        }
        [HttpPost]
        public ActionResult ChangePass(FormCollection fr)
        {
            var ht = new AccountModel
            {
                Username = fr.Get("username"),
                Password = fr.Get("password"),
                confirmPass = fr.Get("confirm_password")
            };
            if (ht.Password != ht.confirmPass)
                return View(ht);
            var model = repository.SelectByID1<Account>(ht.Username);
            model.Password = ht.Password;
            repository.commitchanges();
            return View();
        }

        [HttpGet]
        public ActionResult Details(string id)
        {
            var model = repository.SelectByID1<UserInfo>(id);
            var ht = new userInfoModel
            {
               UserName= model.Username,
               Ten= model.Ten,
               DiaChi = model.DiaChi,
               DienThoai= model.DienThoai,
               cmnd= model.Cmmd,
               GioiTinh= model.GioiTinh,
               NgaySinh= model.NgaySinh,
            };
            return View(ht);
        }
        [HttpPost]
        public ActionResult Details(FormCollection fr)
        {
            var ht = new userInfoModel
            {
                UserName =fr.Get("username"),
                Ten = fr.Get("ten"),
                DiaChi = fr.Get("diachi"),
                Email = fr.Get("email"),
                DienThoai = fr.Get("dienthoai"),
                cmnd = fr.Get("cmnd"),
                GioiTinh = Convert.ToInt32(fr.Get("gioitinh")),
                NgaySinh = fr.Get("ngaysinh"),
            };
           
            var model = repository.SelectByID1<UserInfo>(ht.UserName);
            model.Ten = ht.Ten;
            model.DiaChi = ht.DiaChi;
            model.Email = ht.Email;
            model.DienThoai = ht.DienThoai;
            model.Cmmd = ht.cmnd;
            model.GioiTinh = ht.GioiTinh;
            model.NgaySinh = ht.NgaySinh;
            repository.commitchanges();
            return View();
        }
    }
}
